package com.qst.survey.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qst.survey.entity.QuestionAnswer;
import com.qst.survey.entity.QuestionTopic;
import com.qst.survey.mapper.QuestionAnswerMapper;
import com.qst.survey.service.AdminService;
import com.qst.survey.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date: 2021/3/19 18:43
 * @Description: 答案服务具体实现类
 */
@Service
public class AnswerServiceImpl implements AnswerService {
    @Autowired
    QuestionAnswerMapper answerMapper;

    /**
     * @param topicId:
     * @param stringList:
     * @return boolean
     * @description: TODO 增加多个选项的方法
     */
    @Transactional
    @Override
    public Integer addAnswer(Integer topicId, List<String> stringList) {
        int insert = 0;
        Integer integer = countAnswerNumb(topicId);
        for (int i = 0; i < stringList.size(); i++) {

            QuestionAnswer answer = new QuestionAnswer();
            answer.setTopicId(topicId);
            answer.setNumb(integer + i + 1);
            answer.setTitle(stringList.get(i));
            insert = answerMapper.insert(answer);
        }
        return insert;
    }

    /**
     * @param id: 选项
     * @return void
     * @description: TODO 选择的选项选择数量+1
     */
    @Transactional
    @Override
    public void chooseAnswer(Integer id) {
        answerMapper.chooseAnswer(id);
    }
    /**
     * @param arrayList ：
     * @description  TODO   添加多个选择
     */
    @Override
    public void chooseAnswerList(ArrayList<Integer> arrayList) {
        for (Integer id : arrayList
        ) {
            chooseAnswer(id);
        }
    }
    /**
     * @param topicId:
     * @return java.lang.Integer
     * @description: TODO 统计答案的数量
     */
    @Override
    @Transactional
    public Integer countAnswerNumb(Integer topicId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("topic_id", topicId);
        Integer integer = answerMapper.selectCount(queryWrapper);
        return integer;
    }
    /**
     * @param topicId ：答案id
     * @description  TODO   找到题目全部答案
     * @return java.util.List<com.qst.survey.entity.QuestionAnswer>
     */
    @Override
    @Transactional
    public List<QuestionAnswer> findTopicAllAnswer(Integer topicId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("topic_id", topicId);
        List<QuestionAnswer> list = answerMapper.selectList(queryWrapper);
        System.out.println("选项list:" + list);
        return list;
    }
    /**
     * @param topicId ：答案id
     * @description  TODO   查找答案数据
     * @return java.util.List<com.qst.survey.entity.QuestionAnswer>
     */
    @Override
    public List<QuestionAnswer> findAnswerData(Integer topicId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("topic_id", topicId);
        List<QuestionAnswer> list = answerMapper.selectList(queryWrapper);
        return list;
    }
}
